package com.example.pinkcat.client;

import android.content.Context;


import com.example.pinkcat.activity.pinkCatUser.userStaticMsg;

import java.io.IOException;
import java.util.List;
import java.util.Observable;

import okhttp3.Interceptor;
import okhttp3.Response;

public class ReceivedCookiesInterceptor implements Interceptor {
    private Context context;

//    public ReceivedCookiesInterceptor(Context context) {
//        super();
//        this.context = context;
//
//    }

    @Override
    public Response intercept(Chain chain) throws IOException {

        Response originalResponse = chain.proceed(chain.request());
        //这里获取请求返回的cookie
        if (!originalResponse.headers("Set-Cookie").isEmpty()) {
            final StringBuffer cookieBuffer = new StringBuffer();
            List<String> s=originalResponse.headers("Set-Cookie");
            userStaticMsg.JSESSIONID=s.get(0).split(";")[0];
            System.out.println("userStaticMsg.JSESSIONID = 哈哈哈哈" + userStaticMsg.JSESSIONID);
            //最近在学习RxJava,这里用了RxJava的相关API大家可以忽略,用自己逻辑实现即可.大家可以用别的方法保存cookie数据
//            Observable.from(originalResponse.headers("Set-Cookie"))
//                    .map(new Func1<String, String>() {
//                        @Override
//                        public String call(String s) {
//                            String[] cookieArray = s.split(";");
//                            return cookieArray[0];
//                        }
//                    })
//                    .subscribe(new Action1<String>() {
//                        @Override
//                        public void call(String cookie) {
//                            cookieBuffer.append(cookie).append(";");
//                        }
//                    });
//            SharedPreferences sharedPreferences = context.getSharedPreferences("cookie", Context.MODE_PRIVATE);
//            SharedPreferences.Editor editor = sharedPreferences.edit();
//            editor.putString("cookie", cookieBuffer.toString());
//            editor.commit();
        }

        return originalResponse;
    }
}